<?php

	/*
	 * 
				$file_info = pathinfo($_FILES['file_file']['name']);
				
				$handler = $this->db->handler("file_lib", array(
					"name" => $file_info['extension']
				));
				
				if(!move_uploaded_file($_FILES['file_file']['tmp_name'], Yomm_Config::FileSave . $handler . '.'. $file_info['extension'])) {
					return $this->ajaxResponse('<script language="javascript" type="text/javascript">window.top.window.GridApp.ImageFrmSendStop(false, \'Fehler beim Hochladen des Files.\', 0)</script>', false);
				} else{
					$image = SiteConfig::Domain . Yomm_Config::FileSave . $handler . '.'. $file_info['extension'];
					return $this->ajaxResponse('<script language="javascript" type="text/javascript">window.top.window.GridApp.ImageFrmSendStop(true, \'upload_success\', '.$handler.', \''.$image.'\', \''.$this->get->field.'\')</script>', false);
				}
	 */

	/**
	 * @package Yomm
	 * @author bsuter
	 *
	 */
	Class Yomm_Library_FileHandler {
		
		private $db = null;
		
		private $error = null;
		
		private $tmp_obj = null;
		
		public function __construct()
		{
			$this->db = ydsql::getInstance();	
		}
		
		/**
		 * @param $post_file_array [array] item from post $_FILES['__YOUR_NAME__'];
		 */
		public function UploadFile($post_file_array)
		{
			$ext = pathinfo($post_file_array['name'], PATHINFO_EXTENSION);
			
			$handler = $this->db->handler("file_lib", array(
				"name" => $ext,
				"full_name" => $post_file_array['name']
			), false, true);
			
			if(!$handler)
				return false;
				
			if(!move_uploaded_file($post_file_array['tmp_name'], Yomm_Config::DIR_FILES_PATH . $handler . '.' . $ext))
			{
				$error = new Yomm_Error('error while moving file "'.$post_file_array['name'].'" to "'.Yomm_Config::DIR_FILES_PATH.'" in File class.');
				$this->setError($error->getMessage());
				return false;
			}
			
			return $handler;
		}
		
		public function getName($file_id = null) {
			
			if(!is_null($this->tmp_obj))
			{
				$file = $this->tmp_obj;
			} else {
				$file = $this->db->select("file_lib", "full_name", "WHERE id='{$file_id}'", false);
				$this->tmp_obj = $file;
			}
			
			return $file->full_name;
		}
		
		public function getFileIcon($file_id = null)
		{
			if(!is_null($this->tmp_obj))
			{
				$file = $this->tmp_obj;
			} else {
				$file = $this->db->select("file_lib", "*", "WHERE id='{$file_id}'", false);
				$this->tmp_obj = $file;
			}
			
			if(!is_null($file_id) && count($file) !== 1)
			{
				$error = new Yomm_Error("file_id '{$file_id}' not found in mysql table 'file_lib'. sql_numrows is not 1. sql_numrows: {$this->db->sql_numrows}");
				$this->setError($error->getMessage());
				return false;
			}
			
			$icos = scandir(Yomm_Config::DIR_IMAGES_SHARE);
			
			foreach($icos as $k => $v)
			{
				if($v == "." || $v == "..")
					continue;
					
				if($file->name == $v)
					return Yomm_Config::DIR_IMAGES_SHARE . $v . '.gif';
			}
			
			return Yomm_Config::DIR_IMAGES_SHARE . 'file.gif';
		}
		
		/**
		 * get file path for a file id
		 * 
		 * @param $file_id [numeric] ID of the to returning file
		 */
		public function getFile($file_id)
		{
			if(empty($file_id) || !is_numeric($file_id))
			{
				$error = new Yomm_Error('class File function getFile paremeter $file_id is empty or not a file. $file_id: '. $file_id);
				$this->setError($error->getMessage());
				return false;	
			}
			
			if(!is_null($this->tmp_obj))
			{
				$file = $this->tmp_obj;
			} else {
				$file = $this->db->select("file_lib", "*", "WHERE id='{$file_id}'", false);
				$this->tmp_obj = $file;
			}
			
			if(count($file) !== 1)
			{
				$error = new Yomm_Error("file_id '{$file_id}' not found in mysql table 'file_lib'. sql_numrows is not 1. sql_numrows: " . count($file));
				$this->setError($error->getMessage());
				return false;
			}
			
			$path = SiteConfig::Domain . Yomm_Config::DIR_FILES_PATH . $file->id . '.' . $file->name;
			
			
			return $path;
		}
		
		private function setError($message)
		{
			$this->error = $message;
		}
		
		public function getError()
		{
			return $this->error;
		}
		
		public function __destruct()
		{
			$this->tmp_obj = null;
		}
	}